﻿namespace ClassLibrary.DataLayer.DataObjects
{
    using ClassLibrary.BusinessLayer.BusinessObjects;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    public class ClientsDAO : IClientsDAO
    {
        public bool DeleteClients(Clients clsClients)
        {
            return clsClients.Save("D");
        }

        public IList<Clients> GetClients(Clients clsClients)
        {
            DataTable dt = Db.GetDataTable(clsClients.CreateGetCommandParameters());
            clsClients.LoadDataToCollection(dt.Rows[0]);
            if (dt.Rows[0] == null)
            {
                throw new Exception("Clients doesn't exist");
            }
            IList<Clients> list = new List<Clients>();
            list.Add(clsClients);
            return list;
        }

        public IList<Clients> GetClients(string clients)
        {
            Clients clsClients = new Clients();
            clsClients._Client = clients;
            return this.GetClients(clsClients);
        }

        public IList<Clients> GetsClients()
        {
            Clients clsClients = new Clients();
            IDataReader dtReader = Db.GetDataReader(clsClients.CreateGetsCommandParameters());
            IList<Clients> list = new List<Clients>();
            while (dtReader.Read())
            {
                clsClients = new Clients();
                clsClients.LoadDataToCollection(dtReader);
                list.Add(clsClients);
            }
            return list;
        }

        public DataTable GetsClients(string SPName, DbParameter[] param)
        {
            Clients clsClients = new Clients();
            return Db.GetDataTable(clsClients.GetsDataReader(SPName, param));
        }

        public bool InsertClients(Clients clsClients)
        {
            return clsClients.Save("I");
        }

        public int InsertClientsReturnIdentity(Clients clsClients)
        {
            return clsClients.Save("I", true);
        }

        public string InsertClientsReturnOutput(Clients clsClients)
        {
            return clsClients.Save("I", "@ClientsID");
        }

        public IList<Clients> ListClients(Clients clsClients)
        {
            IDataReader dtReader = Db.GetDataReader(clsClients.CreateListCommandParameters());
            IList<Clients> list = new List<Clients>();
            while (dtReader.Read())
            {
                Clients clsClientss = new Clients();
                clsClientss.LoadDataToCollection(dtReader);
                list.Add(clsClientss);
            }
            return list;
        }

        public IList<Clients> ListClients(string clients)
        {
            Clients clsClients = new Clients();
            clsClients._Client = clients;
            return this.ListClients(clsClients);
        }

        public bool UpdateClients(Clients clsClients)
        {
            return clsClients.Save("U");
        }

        public int UpdateClients(string SPName, DbParameter[] param)
        {
            Clients clsClients = new Clients();
            return Db.ExecuteNonQuery(clsClients.UpdateQuery(SPName, param));
        }
    }
}

